﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CTMOES.Models;
using CTMOES.DAL;
using CTMOES.Helper;

namespace CTMAdmin
{
    public partial class DataBox : Form
    {
        public QueModel queModel = null;
        public int mode = 0;

        public DataBox()
        {
            InitializeComponent();
        }

        private void DispalyBox_Load(object sender, EventArgs e)
        {
            if (mode == 1)
            {
                btn_Op.Text = "修改";
                this.Text = "修改数据";
                txt_Des.Text = queModel.ProContent;

                dataGridView1.DataSource = queModel.OptionList;
                dataGridView1.Columns["ID"].Visible = false;
                dataGridView1.Columns["QueID"].Visible = false;
                dataGridView1.Columns["StuAns"].Visible = false;
                dataGridView1.Columns["Description"].FillWeight = 80;
                dataGridView1.Columns["Correct"].FillWeight = 20;
                dataGridView1.Columns["Descriptionx"].Visible = false;
                dataGridView1.Columns["Correctx"].Visible = false;
            }
            cbSubject.DataSource = DataHelper.AllSubject();
            cbSubject.DisplayMember = "Name";
            cbSubject.ValueMember = "ID";
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            Guid id;
            if (mode == 0)
                id = Guid.NewGuid();
            else
                id = queModel.ID;

            OptionModel oModel = null;
            int count = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                oModel = GetOptionsFromRow(row);
                if (oModel != null)
                {
                    oModel.QueID = id;
                    if (mode == 0)
                        DataHelper.AddOp(oModel);
                    else
                        DataHelper.EditOption(oModel);
                    if (oModel.Correct) count++;
                }
            }

            QueModel qModel = new QueModel();
            qModel.ID = id;
            if (count == 1)
                qModel.Category = 0;
            else if (count > 1)
                qModel.Category = 1;

            qModel.ProContent = txt_Des.Text;
            qModel.SubjectID = (Guid)cbSubject.SelectedValue;

            if (mode == 0)
                DataHelper.AddQue(qModel);
            else
                DataHelper.EditQue(qModel);

            this.Close();
        }

        private OptionModel GetOptionsFromRow(DataGridViewRow row)
        {
            OptionModel model = new OptionModel();
            if (mode == 0)
            {
                model.ID = Guid.NewGuid();
                model.Description = (string)row.Cells["Descriptionx"].Value;
                if (model.Description != null)
                {
                    model.Correct = Convert.ToBoolean(row.Cells["Correctx"].Value);
                }
                else
                    model = null;
            }
            else
            {
                model.ID = (Guid)row.Cells["ID"].Value;
                model.Description = (string)row.Cells["Description"].Value;
                if (model.Description != null)
                {
                    model.Correct = Convert.ToBoolean(row.Cells["Correct"].Value);
                }
                else
                    model = null;
            }

            return model;
        }
    }
}
